#include "stdafx.h"

#include "FreezingArea.h"

sf::SoundBuffer* FreezingArea::freezeSoundBuffer = 0;
sf::Sound* FreezingArea::freezeSound = 0;

FreezingArea::FreezingArea(const char* filenameOn, const char* filenameOff, sf::Vector2f p, FreezingAreaType fat, std::vector<const char*> dialogue1Texts, std::vector<bool> dialogue1People) :
mDialogue1People(dialogue1People), mDialogue1Texts(dialogue1Texts)
{
	if (!freezeSoundBuffer)
	{
		freezeSoundBuffer = new sf::SoundBuffer();
		freezeSoundBuffer->loadFromFile("data/sounds/freeze.wav");
		freezeSound = new sf::Sound(*freezeSoundBuffer);
	}

	active = true;
	mOnTexture.loadFromFile(filenameOn);
	mOffTexture.loadFromFile(filenameOff);
	mSprite.setPosition(p);

	type = GOT_NORMAL;

	mFat = fat;

	pos = p;
	size = sf::Vector2f(mOnTexture.getSize().x, mOnTexture.getSize().y);
	mSprite.setOrigin(size / 2.0f);
}

void FreezingArea::Draw(sf::RenderWindow& window)
{
	if(active)
	{
		//GameObject::Draw(window);
		//mSprite.setScale(pulseScale);
		mSprite.setTexture(mOnTexture);
	}
	else
		mSprite.setTexture(mOffTexture);

	mSprite.setOrigin(size / 2.0f);
	window.draw(mSprite);
}

void FreezingArea::OnTouch(Player* player)
{
	if(player->mFreezingArea && player->mFreezingArea->active)
		return;

	Player* other = player==&gFemale ? &gMale : &gFemale;

	bool otherFrozen = other->mFreezingArea && other->mFreezingArea->active;

	if(player == &gMale && mFat == FREEZE_MALE && active && !otherFrozen)
	{
		player->Freeze(this);
		//freezeSound->play();
	}
	if(player == &gFemale && mFat == FREEZE_FEMALE && active && !otherFrozen)
	{
		player->Freeze(this);
		//freezeSound->play();
	}
	if(player == &gMale && mFat == FREEZE_FEMALE && active && otherFrozen)
	{
		if(sf::Keyboard::isKeyPressed(sf::Keyboard::E))
		{
			active = false;
			freezeSound->play();
		}
	}
	if(player == &gFemale && mFat == FREEZE_MALE && active && otherFrozen)
	{
		if(sf::Keyboard::isKeyPressed(sf::Keyboard::RControl))
		{
			active = false;
			freezeSound->play();
		}
	}
}